//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Main.h"
#include "bases.h"
#include "convert.h"
#include "langman.h"

//---------------------------------------------------------------------------
USEFORM("Bases.cpp", BaseForm);
USEFORM("Main.cpp", MainForm);
USEFORM("Help.cpp", HelpForm);
USEFORM("Convert.cpp", ConvertForm);
USEFORM("Config.cpp", ConfigForm);
//---------------------------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
    AnsiString tmp;

    Application->Title= "XCalcJ 3.1";

    HANDLE hInstanceMutex= ::CreateMutex(NULL, true, "Single_XCalcj");

    if(GetLastError() == ERROR_ALREADY_EXISTS)
    {
        if(hInstanceMutex)
            CloseHandle(hInstanceMutex);

        tmp= Application->Title;
        Application->Title= "";

        HWND hPrevApp= ::FindWindow(NULL, tmp.c_str());

        if(hPrevApp)
            PostMessage(hPrevApp, WM_SYSCOMMAND, SC_RESTORE, 0);

        return 0;
    }

    try
    {
         Application->Initialize();
         Application->CreateForm(__classid(TMainForm), &MainForm);
         Application->CreateForm(__classid(TBaseForm), &BaseForm);
         Application->CreateForm(__classid(TConvertForm), &ConvertForm);
         
         tmp= "L_PORT";
         if(MainForm->Lang == 1) tmp= "L_INGL";

         HRSRC hrsrc= FindResource(HInstance, tmp.c_str(), RT_RCDATA);
         HGLOBAL hglob= LoadResource(HInstance, hrsrc);

         TStringList *tt= new TStringList();
         tt->Text= (char*)hglob;

         TLangManager l;
         l.Load(tt, MainForm);
         l.Load(tt, BaseForm);
         l.Load(tt, ConvertForm);
         delete tt;

         Application->Run();
    }
    catch (Exception &exception)
    {
         Application->ShowException(&exception);
    }
    catch (...)
    {
         try
         {
             throw Exception("");
         }
         catch (Exception &exception)
         {
             Application->ShowException(&exception);
         }
    }

    ReleaseMutex(hInstanceMutex);
    CloseHandle(hInstanceMutex);

    return 0;
}
//---------------------------------------------------------------------------
